﻿<%@ WebHandler Language="C#" Class="Image" %>

using System;
using System.Web;
using System.Drawing;
using System.Web.SessionState;

public class Image : IHttpHandler,IRequiresSessionState {

    public void ProcessRequest (HttpContext context) {
        context.Response.ContentType = "image/jpeg";

        Bitmap bitmap = new Bitmap(100, 50);
        Graphics g = Graphics.FromImage(bitmap);
        g.Clear(Color.White);

        Random rand = new Random();

        for (int i = 0; i <= 15; i++)
        {
            int x1 = rand.Next(bitmap.Width);
            int x2 = rand.Next(bitmap.Width);
            int y1 = rand.Next(bitmap.Height);
            int y2 = rand.Next(bitmap.Height);
            g.DrawLine(new Pen(Color.Blue), x1, y1, x2, y2);
        }

        int code = rand.Next(1000, 9999);
        string strCode = code.ToString();
        HttpContext.Current.Session["securitycode"] = strCode;
        g.DrawString(strCode, new Font("宋体", 30), Brushes.Black,new PointF(0, 0));
        bitmap.Save(context.Response.OutputStream, System.Drawing.Imaging.ImageFormat.Jpeg);
    }

    public bool IsReusable {
        get {
            return false;
        }
    }

}